/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * no s
 */
package dao;


import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import model.User;

/**
 *
 * @author user
 */
public class DaoUser {
    
    public List<User> findAllUsers(){
        List<User> users= new ArrayList<User>();
        
        User u= new User();
        u.setUsuario("jmonsalve");
        u.setClave("jmonsalve");
        users.add(u);
        
        u= new User();
        u.setUsuario("obeltran");
        u.setClave("obeltran");
        users.add(u);
         
        u= new User();
        u.setUsuario("jcely");
        u.setClave("jcely");
        users.add(u);
        
        u= new User();
        u.setUsuario("jcardenas");
        u.setClave("jcardenas");
        users.add(u);
        
        return users;
    }
    
    private static final String SQL_INSERT =
            "INSERT INTO usuarios ("
            + "Usuario,Nombre,Clave,Correo,PJugadas,PGanadas,Fichas"
            + ") VALUES (?, ?, ?, ?, ?, ?, ?)";
    private static final String SQL_SELECT =
            "SELECT * "
            + " FROM usuarios where Usuario= ?";
    private static final String SQL_SELECT_All = 
            "SELECT * "
            + " FROM usuarios";
    private static final String SQL_UPDATE =
            "UPDATE usuarios SET "
            + "Nombre = ?,Clave = ?,Correo = ?,PJugadas = ?,PGanadas = ?,Fichas = ?"
            + " WHERE "
            + "Usuario = ? ";

    /* SQL to delete data */
    private static final String SQL_DELETE =
            "DELETE FROM usuarios WHERE "
            + "Usuario = ?";
    
    private static final String SELECCIONAR_CORREO =
            "SELECT * FROM usuarios WHERE "
            + "Correo = ?";
    
    private static final String SELECCIONAR_NOMBRE =
            "SELECT * FROM usuarios WHERE "
            + "Nombre = ?";
    
  
 
    
    
    
   
    public void create(User user, Connection conn) throws SQLException {

        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(SQL_INSERT);
           // ps.setInt(1, clt.getIdtabla());
            ps.setString(1, user.getUsuario());
            ps.setString(2, user.getNombre());
            ps.setString(3,user.getClave());
            ps.setString(4,user.getCorreo());
            ps.setInt(5, user.getPjugadas());
            ps.setInt(6, user.getPganadas());
            ps.setInt(7, user.getFichas());
           
            ps.executeUpdate();
        } finally {
            cerrar(ps);
            //cerrarCnn(conn);
        }
    }

    public User load(User user, Connection conn) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(SQL_SELECT);
            ps.setString(1,user.getUsuario());
            rs = ps.executeQuery();
            List results = getResults(rs);
            if (results.size() > 0) {
                return (User)results.get(0);
            } else {
                return null;
            }
        } finally {
            cerrarRs(rs);
            cerrar(ps);
            //cerrarCnn(conn);  
        }
   }

    public List loadAll(Connection conn) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(SQL_SELECT_All);
            rs = ps.executeQuery();
            List results = getResults(rs);
            if (results.size() > 0) {
                return results;
            } else {
                return new ArrayList(Arrays.asList(new User()));
            }
        } finally {
            cerrarRs(rs);
            cerrar(ps);           
        }
    }

    public void update(User user, Connection conn) throws SQLException {
        PreparedStatement ps = null;
        try {
            /*            
            ps = conn.prepareStatement(SQL_UPDATE);
            ps.setString(1, clt.getDescripcion());
            ps.setDouble(2, dto.getPrecio());
            ps.setInt(3, clt.getExistencias());
            ps.setString(4, dto.getClaveArticulo());
            ps.executeUpdate();*/
            ps = conn.prepareStatement(SQL_UPDATE);
            ps.setString(1, user.getNombre());
            ps.setString(2, user.getClave());
            ps.setString(3, user.getCorreo());
            ps.setString(4, user.getUsuario());
            ps.setInt(5, user.getPjugadas());
            ps.setInt(6, user.getPganadas());
            ps.setInt(7, user.getFichas());
            ps.executeUpdate();
            
        } finally {
            cerrar(ps);
            cerrarCnn(conn);
        }
    }

    public void delete(User user, Connection conn) throws SQLException {
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(SQL_DELETE);
            ps.setString(1, user.getUsuario());
            ps.executeUpdate();
        } finally {
            cerrar(ps);
            cerrarCnn(conn);
        }
    }

    private List getResults(ResultSet rs) throws SQLException {
        List results = new ArrayList();
        while (rs.next()) {
            User user = new User();
            /*clt.setClaveArticulo(rs.getString("claveArticulo"));
            clt.setDescripcion(rs.getString("descripcion"));
            clt.setPrecio(rs.getDouble("precio"));
            clt.setExistencias(rs.getInt("existencias"));*/
           user.setUsuario(rs.getString("Usuario"));
           user.setNombre(rs.getString("Nombre"));
           user.setClave(rs.getString("Clave"));
           user.setCorreo(rs.getString("Correo"));
           user.setPjugadas(rs.getInt("PJugadas"));
           user.setPganadas(rs.getInt("PGanadas"));
           user.setFichas(rs.getInt("Fichas"));
            
            results.add(user);
        }
        return results;
    }
    
    public boolean existeUsuario(String usuario, Connection conexion) throws SQLException{
        boolean existe = false;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conexion.prepareStatement(SQL_SELECT);
            ps.setString(1, usuario);
            rs = ps.executeQuery();
            return rs.first();
        } finally {
            cerrarRs(rs);
            cerrar(ps);
        }
    }
    
    public boolean existeNombre(String nombreCliente, Connection conexion) throws SQLException{
        boolean existe = false;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conexion.prepareStatement(SELECCIONAR_NOMBRE);
            ps.setString(1, nombreCliente);
            rs = ps.executeQuery();
            return rs.first();
        } finally {
            cerrarRs(rs);
            cerrar(ps);
        }
    }
    
    public boolean existeCorreo(String identificacion, Connection conexion) throws SQLException{
        boolean existe = false;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conexion.prepareStatement(SELECCIONAR_CORREO);
            ps.setString(1, identificacion);
            rs = ps.executeQuery();
            return rs.first();
        } finally {
            cerrarRs(rs);
            cerrar(ps);
        }
    }
    
     public User encontrarUsuarioUsuario(String abr, Connection conn) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(SQL_SELECT);
            ps.setString(1, abr);
            rs = ps.executeQuery();
            List results = getResults(rs);
            if (results.size() > 0) {
                return (User)results.get(0);
            } else {
                return null;
            }
        } finally {
            cerrarRs(rs);
            cerrar(ps);  
        }
    }

    private void cerrar(PreparedStatement ps) throws SQLException {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
            }
        }
    }

    private void cerrarRs(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
    }

    private void cerrarCnn(Connection cnn) {
        if (cnn != null) {
            try {
                cnn.close();
            } catch (SQLException e) {
            }
        }
    }
}

